package org.jsola.hr.helper;

import org.jsola.common.ClassKit;
import org.jsola.common.DateKit;
import org.jsola.common.SafeKit;
import org.jsola.hr.dto.TaskAddDTO;
import org.jsola.hr.dto.TaskUpdateDTO;
import org.jsola.hr.entity.TaskDO;
import org.jsola.hr.query.TaskQuery;
import org.jsola.hr.service.ITaskService;
import org.jsola.hr.vo.TaskVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

import static org.jsola.hr.core.AbstractTest.SITE_ID;



/**
 * 人力定时任务
 *
 * @author zhr
 */
@Component
public class TaskTestHelper extends BaseTestHelper {
    
    @Autowired
    private ITaskService taskService;

    /**
     * 生成add测试数据
     * @return TaskAddDTO
     */
    public TaskAddDTO getTaskAddDTO(){
        TaskAddDTO taskAddDTO = new TaskAddDTO();
        taskAddDTO.setSubjectType(getRandomValue("varchar",100));
        taskAddDTO.setSubjectId(getRandomValue("longtext",0));
        taskAddDTO.setDescription(getRandomValue("varchar",100));
        taskAddDTO.setRunBeanName(getRandomValue("varchar",200));
        taskAddDTO.setRunDate((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        taskAddDTO.setRunType(getRandomValue("varchar",100));
        taskAddDTO.setRunParam(getRandomValue("text",0));
        taskAddDTO.setStatus(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        return taskAddDTO;
    }


    /**
     * 生成update测试数据
     * @return TaskUpdateDTO
     */
    public TaskUpdateDTO getTaskUpdateDTO(TaskVO taskVO){
        return taskService.selectDOById(taskVO.getId(), SITE_ID).to(TaskUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return TaskQuery
     */
    public TaskQuery getTaskQuery() {
        TaskQuery taskQuery = new TaskQuery();
        List<TaskDO> taskDOList = taskService.list(new TaskDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(taskDOList)) {
            TaskDO taskDO = taskDOList.get(0);
            taskQuery = taskDO.to(TaskQuery.class);
            if (taskDO.getRunDate() != null) {
                taskQuery.setRunDateStart(DateKit.getBeforeDay(taskDO.getRunDate(),-1));
                taskQuery.setRunDateEnd(DateKit.getBeforeDay(taskDO.getRunDate(),1));
            }
        }
        return taskQuery;
    }

    /**
     * 保存示例
     * @return TaskVO
     */
    public TaskVO saveTaskExample(){
        TaskAddDTO addDTO = this.getTaskAddDTO();
        TaskVO taskVO = taskService.save(addDTO,buildTokenUser());
        return taskVO;
    }


}

